package com.woniuxy.microgoodsserver.brandmag.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.woniuxy.microcommonserver.bean.gy.BrandInfo;
import com.woniuxy.microcommonserver.bean.gy.GoodCategoryInfo2;
import com.woniuxy.microcommonserver.pageHelper.PageRequest;
import com.woniuxy.microcommonserver.pageHelper.TableResult;
import com.woniuxy.microcommonserver.res.ResultMsg;
import com.woniuxy.microgoodsserver.brandmag.service.BrandService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**品牌管理
 * @author ：苟杨
 * @description：TODO
 * @date ：2021/1/25 15:01
 */
@RequestMapping("brand")
@RestController
public class BrandController {

    @Resource
    private BrandService brandServiceImpl;
    /**新增品牌

     * @params:
     * @return
     */
    @PostMapping(value = "/addBrand",produces = MediaType.APPLICATION_JSON_VALUE)
    public String addBrand(BrandInfo brandInfo){
        //判断是否存在
        BrandInfo brandInfo1 = brandServiceImpl.findByName(brandInfo);
        if(brandInfo1 != null){
            //说明存在
            return JSONObject.toJSONString(ResultMsg.builder().code(201).msg("该子目类品牌已经存在!").build());
        }else {
            //说明不存在
            brandServiceImpl.addBrand(brandInfo);
            return JSONObject.toJSONString(ResultMsg.builder().code(200).msg("添加成功").build());
        }
    }

    /**
        查询所有的品牌及其所属子目类
     * @params:
     * @return
     */
    @GetMapping(value = "/findBrand",produces = MediaType.APPLICATION_JSON_VALUE)
    public TableResult findBrand(PageRequest pageRequest){
        PageInfo<BrandInfo> pageInfo = brandServiceImpl.findBrand(pageRequest);
        return new TableResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
        删除品牌类型
     * @params:
     * @return
     */
    @DeleteMapping(value = "/deleteBrand",produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteBrand(BrandInfo brandInfo){
        brandServiceImpl.deleteBrand(brandInfo);
        return JSONObject.toJSONString(ResultMsg.builder().code(200).msg("删除成功").build());
    }

    /**
        根据ID查询品牌信息
     * @params:
     * @return
     */
    @GetMapping(value = "/findById",produces = MediaType.APPLICATION_JSON_VALUE)
    public String findById(BrandInfo brandInfo){
        BrandInfo brandInfo1 = brandServiceImpl.findById(brandInfo);
        return JSONObject.toJSONString(brandInfo1);
    }

    /**
        修改品牌信息
     * @params:
     * @return
     */
    @PutMapping(value = "/updateBrand",produces = MediaType.APPLICATION_JSON_VALUE)
    public String updateBrand(BrandInfo brandInfo,String name){
        brandServiceImpl.updateBrand(brandInfo,name);
        return JSONObject.toJSONString(ResultMsg.builder().code(200).msg("修改成功").build());
    }

    /**
        根据子目ID查询下面所有的所有品牌
     * @params:
     * @return
     */
    @GetMapping(value = "/findBrandBySort",produces = MediaType.APPLICATION_JSON_VALUE)
    public String findBrandBySort(GoodCategoryInfo2 goodCategoryInfo2){
        List<BrandInfo> brands = brandServiceImpl.findBrandBySort(goodCategoryInfo2);
        return JSONObject.toJSONString(brands);
    }
}
